package jmeterBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import impl.TestServerBusinessImpl;
import models.HttpResponse;
import models.MyConfig;
import models.ReturnJson;
import tools.Http;
import tools.ReadProperties;
import tools.WriteProperties;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/1/28.
 * jmeter node地址管理
 */
public class JmeterNodeManager implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        //读取请求
        String action = request.getParameter("action");
        //action=getNodes
        if (Objects.equals(action, "getNodes")){
            String appPath = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
            ReadProperties rp = new ReadProperties(appPath);
            String[] nodes = rp.getProperties().getProperty("jmeterNodeAddress").split(",");
            JSONArray jaNodes = new JSONArray();
            Collections.addAll(jaNodes, nodes);
            returnJson.setResult(jaNodes);
        }
        //action=checkNode&address=192.168.0.0:8080
        if (Objects.equals(action, "checkNode")){
            String address = "http://" + request.getParameter("address") + "/test1";
            Http http = new Http();
            HttpResponse hp = http.sendRequest("get", null, address, "", 5000);
            if (!Objects.equals("ok", hp.getResponseResult())){
                returnJson.setApiStatus(2);
                returnJson.setErrorMsg("node connect exception");
            }
            //ApiStatus返回1 连接成功
        }
        if (Objects.equals(action, "addNode")){
            String address = request.getParameter("address");
            String appPath = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
            try {
                WriteProperties.addElement(appPath, "jmeterNodeAddress", address);
            }catch (Exception e){
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("WriteProperties.addElement exception");
            }
        }
        if (Objects.equals(action, "removeNode")){
            String address = request.getParameter("address");
            String appPath = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
            try {
                WriteProperties.removeElement(appPath, "jmeterNodeAddress", address);
            }catch (Exception e){
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("WriteProperties.removeElement exception");
            }
        }
        if (Objects.equals(action, "getNodeLogList")){
            String address = "http://" + request.getParameter("address") + "/jmeter/jmeterLogManager";
            String param = "action=getHtmlList";
            Http http = new Http();
            HttpResponse hp = http.sendRequest("get", null, address, param, 5000);
            JSONObject jsRes = JSON.parseObject(hp.getResponseResult());
            returnJson.setApiStatus(1);
            returnJson.setResult(jsRes.getJSONArray("result"));
        }


        return returnJson.toString();
    }
}
